// 智能悬浮窗
document.addEventListener("DOMContentLoaded", function () {
  const bar = document.getElementById('smartBar');
  if (!bar) return;

  function syncOnLoad(){
    const y = window.pageYOffset || document.documentElement.scrollTop;
    if (y > 0) bar.classList.add('show'); else bar.classList.remove('show');
  }
  syncOnLoad();

  let lastY = window.pageYOffset || document.documentElement.scrollTop;
  let ticking = false;

  function onScroll() {
    const y = window.pageYOffset || document.documentElement.scrollTop;
    if (y > lastY) {
      bar.classList.add('show');
    } else if (y < lastY) {
      bar.classList.remove('show');
    }
    lastY = y <= 0 ? 0 : y;
  }

  window.addEventListener('scroll', function () {
    if (!ticking) {
      requestAnimationFrame(() => { onScroll(); ticking = false; });
      ticking = true;
    }
  }, { passive: true });

  const toTopBtn = document.getElementById('toTopBtn');
  if (toTopBtn) {
    toTopBtn.addEventListener('click', function (e) {
      e.preventDefault();
      window.scrollTo({ top: 0, behavior: 'smooth' });
    });
  }

    // 上一页按钮
  const goBackBtn = document.getElementById('go_back');
  if (goBackBtn) {
    goBackBtn.addEventListener('click', function (e) {
      e.preventDefault();
      if (document.referrer !== "") {
        history.back();        // 有来源页 → 返回上一页
      } else {
        window.location.href = "/";  // 没有来源页 → 回首页
      }
    });
  }

});
